/**
 * Created by hxpeng on 2017/4/18.
 */
(function () {
    'use strict';

    angular.module('App.pages.dynamic')
        .controller('DynamicCtrl', DynamicCtrl);

    function DynamicCtrl($rootScope,store,$scope,$state,AppUtils,DynamicModel,$location,$http,Global,$timeout) {

        //效果事件
        //页面进入先绑定事件，（即使是缓存页面也会执行）
        $scope.$on('$ionicView.enter', function(){
            $('a.target-burger').unbind("click").bind("click",function(e){
                if(!$('nav.main-nav').hasClass('toggled')){
                    var timer1 = $timeout(
                        function() {
                            $('#btn1').toggleClass('toggled');
                        },
                        0
                    );
                    var timer2 = $timeout(
                        function() {
                            $('#btn2').toggleClass('toggled');
                        },
                        50
                    );
                    var timer3 = $timeout(
                        function() {
                            $('#btn3').toggleClass('toggled');
                        },
                        100
                    );
                    $('nav.main-nav, a.target-burger').addClass('toggled');
                }else{
                    var timer1 = $timeout(
                        function() {
                            $('#btn3').removeClass('toggled');
                            $('#btn2').removeClass('toggled');
                            $('#btn1').removeClass('toggled');
                        },
                        100
                    );
                    timer1.then(
                        function() {
                            $('nav.main-nav, a.target-burger').removeClass('toggled');
                        }
                    );
                }

                e.preventDefault();
            })
        });

        $scope.$on('$ionicView.beforeLeave', function(){
            $('a.target-burger').click();
        });

        $scope.createNewDynamic = function(){
            $location.path('/dynamic/create');
        }


        //逻辑代码
        var vm = this;
        vm.loginAccountVo = {};
        vm.exhortaions = [];
        vm.loadMoreBtnStatus = true;
        vm.loadMoreBtnStr = "加载更多";
        vm.pageIndex = 1;//第一页



        LoginAccountAuth.getAccount()
            .then(function(data){
                vm.loginAccountVo = data;
                vm.getDynamic();
            })


        vm.getDynamic = function(){
            DynamicModel.dynamicList({
                classId: vm.loginAccountVo.classId
            }).$promise.then(function(response){
                var reponseDate = response.pageResult;
                var items = reponseDate.pageResult;
                var pageIndex = reponseDate.pageIndex;
                var pageTotalPageSize = reponseDate.pageTotalPageSize;
                if(pageIndex == pageTotalPageSize || items.length == 0){
                    vm.loadMoreBtnStatus = false;
                    vm.loadMoreBtnStr = "没有更多数据了";
                }
                angular.forEach(items,function(data){
                    vm.exhortaions.push(data);
                })
            })
        }




    }

})();
